package com.privacy.accountmgr

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast

import com.privacy.accountmgr.utils.PreferenceHelper
import com.privacy.accountmgr.utils.md5
import com.privacy.accountmgr.views.numberpasswordview.NumberPasswordView
import org.yuangu.privacyfinance.activities.StartupActivity

class LockActivity : Activity() {
    private var numberPasswordView: NumberPasswordView? = null
    private var firstCode: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lock2)
        initView()
    }

    private fun initView() {
        numberPasswordView = findViewById(R.id.nk)// 数字键盘
        findViewById<View>(R.id.iv_more).setOnClickListener { _ ->
            // startActivity(Intent(this@LockActivity, AboutActivity::class.java))
        }
        if (TextUtils.isEmpty(PreferenceHelper.getString(PWD, ""))) {
            numberPasswordView!!.setTip(resources.getString(R.string.create_password))
        } else {
            numberPasswordView!!.setTip(resources.getString(R.string.enter_password))
        }
        numberPasswordView!!.setOnInputNumberCodeCallback(
                object : NumberPasswordView.OnInputNumberCodeCallback {
                    override fun onResult(code: String) {
                        val password = PreferenceHelper.getString(PWD, "")
                        if (TextUtils.isEmpty(password)) {
                            if (TextUtils.isEmpty(firstCode)) {
                                firstCode = code
                                numberPasswordView!!.setTip(resources.getString(R.string.confirm_password))
                                numberPasswordView!!.clearNumber()
                            } else {
                                if (code == firstCode) {
                                    PreferenceHelper.setString(PWD, code.md5())
                                    Toast.makeText(this@LockActivity, resources.getString(R.string.set_up_success), Toast.LENGTH_SHORT).show()
                                    startActivity(Intent(this@LockActivity, StartupActivity::class.java))
                                    finish()
                                } else {
                                    Toast.makeText(this@LockActivity, resources.getString(R.string.password_not_match), Toast.LENGTH_SHORT).show()
                                    numberPasswordView!!.setTip(resources.getString(R.string.create_password))
                                    firstCode = null
                                    numberPasswordView!!.postDelayed({ numberPasswordView!!.clearNumber() }, 200)
                                }

                            }
                        } else {
                            if (password == code.md5()) {
                                startActivity(Intent(this@LockActivity, StartupActivity::class.java))
                                finish()
                            } else {
                                numberPasswordView!!.setTip(resources.getString(R.string.password_wrong))
                                numberPasswordView!!.postDelayed({ numberPasswordView!!.clearNumber() }, 200)
                            }
                        }
                    }

                })
    }

    companion object {
        private val PWD = "pwd"
    }

}